#!/bin/bash

echo "🔧 编译并启动 TypeScript 后端服务器..."
echo "============================================"

# 检查是否存在之前的编译文件
if [ -d "server/dist" ]; then
    echo "🗑️  清理旧的编译文件..."
    rm -rf server/dist
fi

echo "📦 编译 TypeScript 文件..."
npx tsc server/index.ts --outDir server/dist --module commonjs --target es2020 --esModuleInterop --allowSyntheticDefaultImports --resolveJsonModule

if [ $? -eq 0 ]; then
    echo "✅ 编译成功!"
    echo ""
    echo "🚀 启动后端服务器..."
    node server/dist/index.js
else
    echo "❌ 编译失败!"
    exit 1
fi
